//
//  RCServerActivityListCell.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-28.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCServerActivityListCell.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "UIImageView+WebCache.h"
#import "NSString+NSString_Ex_.h"
#import "TTTAttributedLabel.h"
#import "NSAttributedString+LVAddition.h"


#define kDefaultImageFile        @"image_default.png"
#define kBGImageFile            @"image_discount_cell_bg.png"
#define kDefaultimageFile        @"image_recommendFood_image_bg.png"

@implementation RCServerActivityListCell{
    UILabel *_nameLabel;
    
    UIImageView *_picImageView;
    
    UILabel *_orginalPriceLabel;
    
    UILabel *_presentWordLabel;
    UILabel *_presentPriceLabel;
    UILabel *_dataTimeLabel;
    
    UIButton *_attentCountBtn;
    
}

@synthesize delegate = _delegate;
@synthesize activityID = _activityID;


- (void) dealloc
{
    _delegate = nil;
    KF_RELEASE_SAFELY(_activityID);
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        [self setBackgroundColorWithImage:bgImage];
        self.backgroundColor = [UIColor clearColor];
        
        [self createPlaceHolder];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void) createPlaceHolder
{
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 16, 291, 19)];
    [self.contentView addSubview:_nameLabel];
    _nameLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:19];
    _nameLabel.textColor = [UIColor colorWithHex:0xff505050];
    [_nameLabel setBackgroundColor:[UIColor clearColor]];
    [_nameLabel release];
    
    UIImageView *defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 40, 115, 79)];
    defaultImageView.backgroundColor = [UIColor clearColor];
    UIImage *defaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_recommendFood_image_bg.png" ofType:nil]];
    defaultImage = [defaultImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [defaultImageView setImage:defaultImage];
    [self.contentView addSubview:defaultImageView];
    [defaultImageView release];
    
    _picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 43, 109, 73)];
    [self.contentView addSubview:_picImageView];
    [_picImageView release];
    
    _orginalPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(139, 51, 115, 12)];
    [self.contentView addSubview:_orginalPriceLabel];
    _orginalPriceLabel.font = [UIFont systemFontOfSize:12];
    _orginalPriceLabel.textColor = [UIColor colorWithHex:0xff707070];
    [_orginalPriceLabel setBackgroundColor:[UIColor clearColor]];
    [_orginalPriceLabel release];
    
    _presentWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 51, 50, 12)];
    _presentWordLabel.backgroundColor = [UIColor clearColor];
    _presentWordLabel.textColor = [UIColor colorWithHex:0xff707070];
    _presentWordLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _presentWordLabel.text = @"现价：";
    CGSize size = [_presentWordLabel.text sizeWithFont:_presentWordLabel.font constrainedToSize:CGSizeMake(200, 20)];
    _presentWordLabel.frame = CGRectMake(200, 51, size.width, 12);
    [self.contentView addSubview:_presentWordLabel];
    
    _presentPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 51, 45, 12)];
    [self.contentView addSubview:_presentPriceLabel];
    _presentPriceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _presentPriceLabel.textColor = kNormalOrangeTextColor;
    [_presentPriceLabel setBackgroundColor:[UIColor clearColor]];
    [_presentPriceLabel release];
    

    _dataTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(139, 71, 145, 12)];
    [self.contentView addSubview:_dataTimeLabel];
    _dataTimeLabel.font = [UIFont systemFontOfSize:12];
    _dataTimeLabel.textColor = [UIColor colorWithHex:0xff707070];
    [_dataTimeLabel setBackgroundColor:[UIColor clearColor]];
    [_dataTimeLabel release];
    
    
    
    _attentCountBtn = [[UIButton alloc] initWithFrame:CGRectMake(139, 88, 115, 27)];
    [self.contentView addSubview:_attentCountBtn];
    [_attentCountBtn addTarget:self action:@selector(attentCountbtnClick) forControlEvents:UIControlEventTouchUpInside];
    _attentCountBtn.titleLabel.font = [UIFont fontWithName:kFontNameHeiti size:12];
    [_attentCountBtn setBackgroundImage:[UIImage imageNamed:@"btn-bg-yellow"] forState:UIControlStateNormal];
    [_attentCountBtn release];
    
    
    UIImageView *horizonLine = [[UIImageView alloc]initWithFrame:CGRectMake(0, 134, 320, 1)];
    horizonLine.backgroundColor = [UIColor clearColor];
    [horizonLine setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_line_type3.png" ofType:nil]]];
    [self.contentView addSubview:horizonLine];
    [horizonLine release];
    
}

- (void) bindData:(RCServerActivityItemDataModel *)item
{
    _nameLabel.text = item.title;
    UIImage *img = [UIImage imageNamed:@"image_member_cardType01"];

    [_picImageView setImageWithURL:[NSURL URLWithString:item.picUrl] placeholderImage:img];
    _orginalPriceLabel.text = [NSString stringWithFormat:@"原价：￥%@",item.originalprice];
    CGSize size = [_orginalPriceLabel.text sizeWithFont:_orginalPriceLabel.font constrainedToSize:CGSizeMake(2000, 20)];
    CGRect rect = _orginalPriceLabel.frame;
    rect.size.width = size.width;
    _orginalPriceLabel.frame = rect;
    
    
    rect = _presentWordLabel.frame;
    rect.origin.x = CGRectGetMaxX(_orginalPriceLabel.frame)+10;
    _presentWordLabel.frame =rect;
    
    rect = _presentPriceLabel.frame;
    rect.origin.x = CGRectGetMaxX(_presentWordLabel.frame);
    _presentPriceLabel.frame = rect;
    
    _presentPriceLabel.text = [NSString stringWithFormat:@"￥%@",item.presentprice];
    _dataTimeLabel.text = [NSString stringWithFormat:@"截止日期：%@",item.datetime];
    
    [_attentCountBtn setTitle:[NSString stringWithFormat:@"参与人数%@",item.num] forState:UIControlStateNormal];
    if ([item.num intValue] == 0) {
        self.activityID = nil;
    }else{
        self.activityID = item.aID;
    }

}

- (void)attentCountbtnClick
{
    if ([_delegate respondsToSelector:@selector(didClickAtActivity:)]) {
        [_delegate didClickAtActivity:self.activityID];
    }
}

@end


@implementation RCServerActivityItemCell{
    UILabel *_titleLabel;
    TTTAttributedLabel *_contentLabel;
    UIImageView *_contentImageView;
    UIImageView *_defaultImageView;
    
}
@synthesize contentLabel = _contentLabel;
//@synthesize contentImageView = _contentImageView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self createPlaceHolder];
    }
    return self;
}


- (void) createPlaceHolder
{
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 16, 250, 15)];
    [self.contentView addSubview:_titleLabel];
    _titleLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:15];
    _titleLabel.textColor = [UIColor colorWithHex:0xff656565];
    [_titleLabel setBackgroundColor:[UIColor clearColor]];
    [_titleLabel release];
    
    
    
    _contentLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(14, 36, 292, 60)];
    [self.contentView addSubview:_contentLabel];
    _contentLabel.numberOfLines = 0;
    _contentLabel.leading = 10;
    _contentLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _contentLabel.textColor = [UIColor colorWithHex:0xff656565];
    [_contentLabel setBackgroundColor:[UIColor clearColor]];
    [_contentLabel release];
    
    _defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 34, 292, 147)];
    _defaultImageView.backgroundColor = [UIColor clearColor];
    UIImage *defaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDefaultimageFile ofType:nil]];
    defaultImage = [defaultImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [_defaultImageView setImage:defaultImage];
    [self.contentView addSubview:_defaultImageView];
    
    _contentImageView = [[UIImageView alloc] initWithFrame:CGRectMake(18, 38, 284, 139)];
    [self.contentView addSubview:_contentImageView];
    [_contentImageView release];
    
}


- (void) bindData:(RCActivityItemInfo *)item
{
    _titleLabel.text = item.title;

    
    if (![NSString stringIsNullOrEmpty:item.content]) {
        _contentLabel.hidden = NO;
        _contentImageView.hidden = YES;
        _defaultImageView.hidden = YES;
        _contentLabel.text = item.content;
        
        CGSize size = [_contentLabel.attributedText sizeWithConstrainedToSize:CGSizeMake(292, 2000) Leading:10];
        CGRect rect = _contentLabel.frame;
        rect.size.height = size.height;
        _contentLabel.frame = rect;
        
    }else if(![NSString stringIsNullOrEmpty:item.picUrl]){
        _contentLabel.hidden = YES;
        _contentImageView.hidden = NO;
        _defaultImageView.hidden = NO;
        [_contentImageView setImageWithURL:[NSURL URLWithString:item.picUrl] placeholderImage:nil];
    }
}


+ (CGFloat) heightForCell:(RCActivityItemInfo *)item
{
   RCServerActivityItemCell *cell =   [[RCServerActivityItemCell alloc] init];
    [cell bindData:item];
    CGFloat height = 0;

    if (![NSString stringIsNullOrEmpty:item.content]) {
      height = cell.contentLabel.frame.origin.y + cell.contentLabel.frame.size.height;
    }else if(![NSString stringIsNullOrEmpty:item.picUrl]){
        height = 181;
    }
    [cell release];
    return height;
}

@end